package com.antler.smt.admin.service.impl;

import com.antler.smt.admin.mapper.*;
import com.antler.smt.constant.RoleConstant;
import com.antler.smt.entity.PurchaserAccountEntity;
import com.antler.smt.entity.PurchaserChargeEntity;
import com.antler.smt.entity.UserEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.request.admin.ConditionSeeKBaseReq;
import com.antler.smt.request.admin.GetRequirementListReq;
import com.antler.smt.response.admin.*;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.ConditionSeekService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 条件查询实现
 *
 * @author zhangyi
 * @date 2018/5/11 17:43
 */
@Service
public class ConditionSeekServiceImpl implements ConditionSeekService {

    @Autowired
    private ConditionSeekMapper conditionSeekMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Autowired
    private RequirementInfoMapper requirementInfoMapper;

    @Autowired
    private PurchaserAccountInfoMapper purchaserAccountInfoMapper;

    @Autowired
    private PurchaserChargeInfoMapper purchaserChargeInfoMapper;

    @Autowired
    private UserEntityMapper userEntityMapper;

    @Autowired
    private BusinessInfoMapper businessInfoMapper;

    /**
     * 条件查询服务商
     *
     * @param req
     * @return
     */
    @Override
    public Result<PageInfo<GetSupplierListResp>> getSupplierInfo(ConditionSeeKBaseReq req) {
        Result result = null;
        PageHelper.startPage(req.getPageCurrent(), req.getPageSize());

        String email = req.getEmail();
        String mobile = req.getMobile();

        if(email != null && email != "" && email != "null" && email != "string"){
            req.setMobile(null);
        }else if(mobile != null && mobile != "" && mobile != "null" && mobile != "string"){
            req.setEmail(null);
        }


        Page<GetSupplierListResp> supplierListInfo = conditionSeekMapper.selectSupplierSeek(req);

        for (int i = 0; i < supplierListInfo.size(); i++){
            GetSupplierListResp supplierListResp = supplierListInfo.get(i);
            int orderCount = orderInfoMapper.selectOrderBySupplierId(supplierListResp.getId());
            supplierListInfo.get(i).setOrderNumber(orderCount);

            //插入项目专员真实名称
            //优质审核项目专员
            UserEntity userEntity = userEntityMapper.selectByPrimaryKey(supplierListResp.getAuditUserId());
            if(userEntity != null){
                supplierListInfo.get(i).setAuditUserRealName(userEntity.getRealName());
            }
             //入驻项目专员
            UserEntity userEntity1 = userEntityMapper.selectByPrimaryKey(supplierListResp.getSettledAuditUserId());
            if(userEntity1 != null){
                supplierListInfo.get(i).setSettledAuditUserRealName(userEntity1.getRealName());
            }
        }
        if (supplierListInfo != null) {
            PageInfo<GetUserListResp> listPageInfo = new PageInfo(supplierListInfo);
            result = new Result(listPageInfo);
        } else {
            result = new Result(ResultCodeEnum.QUERY_DATA_ERROR, "查询数据为空！");
        }
        return result;
    }

    /**
     *  条件查询采购商
     *
     * @param req
     * @return
     */
    @Override
    public Result<PageInfo<GetPurchaserVipInfoListResp>> getPurchaserInfo(ConditionSeeKBaseReq req) {
        Result result = null;
        PageHelper.startPage(req.getPageCurrent(), req.getPageSize());

        String email = req.getEmail();
        String mobile = req.getMobile();

        if(email != null && email != "" && email != "null" && email != "string"){
            req.setMobile(null);
        }else if(mobile != null && mobile != "" && mobile != "null" && mobile != "string"){
            req.setEmail(null);
        }

        Page<GetPurchaserVipInfoListResp> purchaserInfoList = conditionSeekMapper.selectPurchaserSeek(req);
        for(int i = 0; i < purchaserInfoList.size(); i++){
            GetPurchaserVipInfoListResp purchaserList = purchaserInfoList.get(i);
            //获取其订单数量
            GetRequirementListReq requirementListReq = new GetRequirementListReq();
            requirementListReq.setPurchaserId(purchaserList.getId());
            List<GetRequirementListResp> requirementList = requirementInfoMapper.listRequirementInfo(requirementListReq);
            //获取订单数量
            long size = requirementList.size();
            PurchaserAccountEntity purchaserAccount =
                    purchaserAccountInfoMapper.selectPurchaserAccountByMemberId(purchaserList.getMemberId());
            purchaserInfoList.get(i).setVipAndCount("v"+purchaserAccount.getVipLevel()
                    +"/"+purchaserAccount.getCashCoupon()+"张");
            purchaserInfoList.get(i).setOrderCount(size);

            //后台实际审核的总金额
            Long totalMoney = purchaserChargeInfoMapper.getTotalMoney(purchaserList.getId());
            purchaserInfoList.get(i).setTotalMoney(totalMoney);
            //添加最后一次审核的金额
            Long lastAuditResult = purchaserChargeInfoMapper.getLastAuditResult(purchaserList.getId());
            purchaserInfoList.get(i).setLastMoney(lastAuditResult);
        }

        if (purchaserInfoList != null) {
            PageInfo<GetUserListResp> listPageInfo = new PageInfo(purchaserInfoList);
            result = new Result(listPageInfo);
        } else {
            result = new Result(ResultCodeEnum.QUERY_DATA_ERROR, "查询采购商数据列表为空！");
        }
        return result;
    }

    /**
     * 分类获取订单列表
     *
     * @param req
     * @return
     */
    @Override
    public Result<PageInfo<OrderManageListResp>> getOrderInfo(ConditionSeeKBaseReq req,Long userId) {
        Result result = null;
        PageHelper.startPage(req.getPageCurrent(), req.getPageSize());
        Page<OrderManageListResp> orderManageListResps = conditionSeekMapper.selectOrderSeek(req);

        String email = req.getEmail();
        String mobile = req.getMobile();

        if(email != null && email != "" && email != "null" && email != "string"){
            req.setMobile(null);
        }else if(mobile != null && mobile != "" && mobile != "null" && mobile != "string"){
            req.setEmail(null);
        }
        //判断当前是否是商务专员
        Long businessId = businessInfoMapper.getBusinessId(userId);
        Page<OrderManageListResp> orderListBusinessList = new Page<>();
        if(RoleConstant.BUSINESS_USER.equals(businessId)){
            for (int i = 0; i < orderManageListResps.size(); i++){
                if(userId.equals(orderManageListResps.get(i).getBusinessAffairsUserId())){
                    orderListBusinessList.add(orderManageListResps.get(i));
                }
            }
            if (orderListBusinessList != null) {
                PageInfo<GetUserListResp> listPageInfo = new PageInfo(orderListBusinessList);
                return new Result(listPageInfo);
            } else {
                return new Result(ResultCodeEnum.QUERY_DATA_ERROR, "获取采购商数据列表为空！");
            }

        }
        if (orderManageListResps != null) {
            PageInfo<GetUserListResp> listPageInfo = new PageInfo(orderManageListResps);
            result = new Result(listPageInfo);
        } else {
            result = new Result(ResultCodeEnum.QUERY_DATA_ERROR, "查询采购商数据列表为空！");
        }

        return result;
    }

    /**
     * 获取采购商分类待审核列表
     *
     * @param conditionSeeKBaseReq
     * @return
     */
    @Override
    public Result<PageInfo<PurchaserChargeEntity>> getPurchaserWaitChargeList(ConditionSeeKBaseReq conditionSeeKBaseReq) {
        Result result = null;
        PageHelper.startPage(conditionSeeKBaseReq.getPageCurrent(), conditionSeeKBaseReq.getPageSize());

        Page<PurchaserChargeEntity> waitAuditList = conditionSeekMapper.getPurchaserWaitChargeList(conditionSeeKBaseReq);

        for (int i = 0; i < waitAuditList.size(); i++) {
            PurchaserChargeEntity purchaserList = waitAuditList.get(i);
            //获取其订单数量
            GetRequirementListReq requirementListReq = new GetRequirementListReq();
            requirementListReq.setPurchaserId(purchaserList.getPurchaserId());

            List<GetRequirementListResp> requirementList = requirementInfoMapper.listRequirementInfo(requirementListReq);

            //获取订单数量
            long size = requirementList.size();
            PurchaserAccountEntity purchaserAccount =
                    purchaserAccountInfoMapper.selectPurchaserAccountByMemberId(purchaserList.getMemberId());

            waitAuditList.get(i).setVipAndCount("v" + purchaserAccount.getVipLevel()
                    + "/" + purchaserAccount.getCashCoupon() + "张");

            waitAuditList.get(i).setOrderCount(Integer.parseInt(String.valueOf(size)));

            //后台实际审核的总金额
            Long totalMoney = purchaserChargeInfoMapper.getTotalMoney(purchaserList.getPurchaserId());
            waitAuditList.get(i).setTotalMoney(totalMoney);
            //添加最后一次审核的金额
            Long lastAuditResult = purchaserChargeInfoMapper.getLastAuditResult(purchaserList.getPurchaserId());
            waitAuditList.get(i).setLastMoney(lastAuditResult);
        }

        if (waitAuditList != null) {
            PageInfo<PurchaserChargeEntity> listPageInfo = new PageInfo(waitAuditList);
            result = new Result(listPageInfo);
        } else {
            result = new Result(ResultCodeEnum.QUERY_DATA_ERROR, "查询采购商待审核列表数据为空！");
        }
        return result;
    }
}
